package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.service.serviceimpl.manage.steelplant.SteelplantSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Zephyr
 * @description:
 * @date 2019/10/28
 */
@Controller
@RequestMapping(value = "/mb_api/usePart")
public class MbUsePartController extends MbBaseController {

    @Autowired
    private SteelplantSettingService steelplantSettingService;


    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsepartType", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getUsepartType(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        try {
            return this.steelplantSettingService.getUsepartType(beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/readUsepart",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult readUsepart(HttpServletRequest request){

        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }

        String offset = body.getString("offset");
        String limit = body.getString("limit");
        String typeId = body.getString("typeId");

        return new ApiResult(steelplantSettingService.getUsepart(typeId, offset, limit));
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/readUsepartRebar",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult readUsepartRebar(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }

        String offset = body.getString("offset");
        String limit = body.getString("limit");
        String usepartId = body.getString("usepartId");
        return new ApiResult(steelplantSettingService.getUsepartRebar(usepartId, offset, limit));
    }
}
